package hflyeer.gitee.com.global;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录拦截器，
 * 有户没有登录则不能访问系统
 */
public class LoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        String path = request.getServletPath();
        if (path.toLowerCase().indexOf("login")!=-1){
            chain.doFilter(request, response);
        }else {
            Object user = request.getSession().getAttribute("USER");
            if (user!=null){
                chain.doFilter(request, response);
                return;
            }else {
                response.sendRedirect(request.getContextPath()+"/login.do");
            }
            chain.doFilter(request, response);
        }

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
